package com.wilschrader.demarche.stages;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Set;

import com.wilschrader.demarche.Fleet;
import com.wilschrader.demarche.Fleet.Type;
import com.wilschrader.demarche.Simulation;
import com.wilschrader.demarche.Stage;
import com.wilschrader.demarche.Unit;
import com.wilschrader.demarche.util.MersenneTwisterFast;

/**
 * AntiFighterBarrage is run prior to full-fledeged space combat to take out any fighters there might be.
 * 
 * @author Wil Schrader
 * @modifications
 * 	08152011	Updates for the unified unit class.
 * 
 */
public class AntiFighterBarrage implements Stage {

	@Override
	public void execute(MersenneTwisterFast mt, LinkedHashMap<Type, Fleet> fleets) {
		Set<Type> fleetSet = fleets.keySet();
		
		for (Type defendingType : fleetSet) {
			Fleet defendingFleet = fleets.get(defendingType);
			
			for (Type attackingType : fleetSet) {
				if (attackingType != defendingType) {
					Fleet attackingFleet = fleets.get(attackingType);
					
					Simulation.debugOutput("Fleet (" + attackingFleet.getName() + ") launching Anti-Fighter Barrage on opposing Fleet (" + defendingFleet.getName() +").");
					
					ArrayList<Unit> destroyers = attackingFleet.getActiveUnits(Unit.Type.Destroyer);
					
					for (Unit destroyer: destroyers) {
						defendingFleet.registerHits(destroyer.doAttack(mt, Unit.Weapon.Special));
					}
				}
			}
		}
		
		for (Type type : fleetSet) {
			fleets.get(type).allocateHits(Unit.Type.Fighter);
		}
	}

}
